package com.donleo.elastic.controller;


import com.donleo.elastic.common.CommonResult;
import com.donleo.elastic.model.Role;
import com.donleo.elastic.service.EsRoleService;
import com.donleo.elastic.service.RoleService;
import com.donleo.elastic.vo.EsRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangd
 * @since 2020-12-18
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private EsRoleService esRoleService;

    @GetMapping("/findAll")
    public CommonResult findAll(){
        List<Role> list = roleService.list();
        return CommonResult.success(list);
    }

    /**
     * 导入es
     * @return
     */
    @GetMapping("/importEsAll")
    public CommonResult importEsAll(){
        int result = esRoleService.importAll();
        return CommonResult.success(result);
    }

    /**
     * 高亮搜索
     * @return  searchHits
     */
    @GetMapping("/searchHit")
    public CommonResult searchHit(String roleName){
        SearchHits searchHits = esRoleService.searchHit(roleName);
        return CommonResult.success(searchHits);
    }

    /**
     * 高亮搜索
     * 只返回esRolemodel数据
     * @return CommonResult
     */
    @GetMapping("/transferSearchHit")
    public CommonResult transferSearchHit(String roleName){
        return esRoleService.transferSearchHit(roleName);
    }

    /**
     * 分页搜索 //待完善
     * @return  Page<EsRole>
     */
    @GetMapping("/searchPage")
    public CommonResult searchPage(String roleName,Integer pageNum,Integer pageSize){
        Page<EsRole> search = esRoleService.search(roleName, pageNum, pageSize);
        return CommonResult.success(search);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteEs")
    public CommonResult deleteEs(@RequestParam Integer id){
        esRoleService.delete(id);
        return CommonResult.success(1);
    }
}
